class Storge {
    interceptors = []
    interceptor(f) {
        if (typeof f === 'function') {
            this.interceptors.push(f)
        }
        return this
    }
    intercept(res) {
        return this.interceptors
            .filter(f => typeof f === 'function')
            .every(f => f(res))
    }

    getCache(key) {
        return new Promise((resolve, reject) => {
            wx.getStorage({
                key: key,
                success: res => this.intercept(res) && resolve(res),
                fail: reject
            });
        });
    }

    removeCache(key) {
        return new Promise((resolve, reject) => {
            wx.removeStorage({
                key: key,
                success: res => this.intercept(res) && resolve(res),
                fail: reject
            });
        });
    }

    setCache(key, value) {
        return new Promise((resolve, reject) => {
            wx.setStorage({
                key: key,
                data: value,
                success: res => this.intercept(res) && resolve(res),
                fail: reject
            });
        });
    }

}
export default new Storge;